﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Drawing.Printing;
using System.IO;
using Microsoft.Win32;
using System.Runtime.InteropServices;
using System.Diagnostics;

namespace RemotePrintConfigration
{
    public partial class Form1 : Form
    {
        private static readonly string FileName = Directory.GetCurrentDirectory() + @"\" + "config.txt";
        public Form1()
        {
            InitializeComponent();
            PrintDocument printDoc = new PrintDocument();
            foreach (string printerName in PrinterSettings.InstalledPrinters)
            {
                //Console.WriteLine(printerName);
                this.comboBox1.Items.Add(printerName);
            }
            PrinterSettings printerSettings = new PrinterSettings();
            string defaultPrinterName = printerSettings.PrinterName;
            this.comboBox1.SelectedItem = printerSettings.PrinterName;
            //Console.WriteLine("默认打印机名称: " + defaultPrinterName);

            //registerCom();
            runbat();
        }

        private void button1_Click(object sender, EventArgs e)
        {

            string withWindowsNewLine = "这是第一行\r\n这是第二行";
            string withEnvironmentNewLine = "这是第一行" + Environment.NewLine + "这是第二行";
            String content = "domain=" + this.textBox1.Text + "\r\n"
                + "domainLink=" + this.textBox2.Text + "\r\n"
                + "printName=" + this.comboBox1.Text + "\r\n"
                + "remotePrint=" + this.checkBox1.Checked + "\r\n"
                + "CADDrawingPdf=" + this.checkBox2.Checked + "\r\n"
                + "CADProcessBom=" + this.checkBox3.Checked + "\r\n"
                + "ExcelProductBom=" + this.checkBox4.Checked + "\r\n" 
                + "companyName=" + this.textBox3.Text;
            File.WriteAllText(FileName,content);
            MessageBox.Show("保存配置成功！");
            this.Close();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            InitDomainConfig();
        }
        public void InitDomainConfig()
        {
            // 指定配置文件的路径
            string configFilePath = FileName;
            try
            {
                if (File.Exists(configFilePath))
                {
                    // 读取文本文件的所有行
                    string[] lines = File.ReadAllLines(configFilePath);

                    // 遍历每一行并处理配置信息
                    foreach (string line in lines)
                    {
                        // 假设配置文件的每一行都是以"键=值"的格式存储
                        string[] keyValue = line.Split('=');
                        if (keyValue.Length == 2)
                        {
                            string key = keyValue[0].Trim();
                            string value = keyValue[1].Trim();
                            // 在这里处理配置信息，例如存储到字典或使用
                            if (key == "domain")
                            {
                                this.textBox1.Text = value;
                            }
                            try
                            {
                                if (key == "domainLink")
                                {
                                    this.textBox2.Text = value;
                                }
                                if (key == "printName")
                                {
                                    this.comboBox1.SelectedItem = value;
                                }
                                if (key == "remotePrint")
                                {
                                    this.checkBox1.Checked = Convert.ToBoolean(value);
                                }
                                if (key == "CADDrawingPdf")
                                {
                                    this.checkBox2.Checked = Convert.ToBoolean(value);
                                }
                                if (key == "CADProcessBom")
                                {
                                    this.checkBox3.Checked = Convert.ToBoolean(value);
                                }
                                if (key == "ExcelProductBom")
                                {
                                    this.checkBox4.Checked = Convert.ToBoolean(value);
                                }
                                if (key == "companyName")
                                {
                                    this.textBox3.Text = value;
                                }
                            }
                            catch (System.Exception e)
                            {
                                MessageBox.Show(e.ToString(), null);
                            }
                        }
                    }
                }                
            }
            catch (IOException e)
            {
                MessageBox.Show("读取配置文件时发生错误: " + e.Message, null);
            }

        }



        private void registerCom()
        {
            // 获取当前.NET版本
            //string version = RuntimeEnvironment.Version.ToString();
            //Console.WriteLine(".NET Version: " + version);

            // 注册COM组件
            //string regasmPath = @"C:\Windows\Microsoft.NET\Framework64\v4.0.30319\regasm.exe"; // 根据实际路径修改
            //string comComponent = @"C:\path\to\your\com\component.dll"; // 指向你的COM组件DLL
            //try
            //{
            //    ProcessStartInfo startInfo = new ProcessStartInfo(regasmPath, $"{comComponent} /codebase")
            //    {
            //        UseShellExecute = true
            //    };
            //    Process.Start(startInfo).WaitForExit(); // 等待regasm执行完成
            //    Console.WriteLine("COM component registered successfully.");
            //}
            //catch (Exception ex)
            //{
            //    Console.WriteLine("Error registering COM component: " + ex.Message);
            //}
            //using (RegistryKey ndpKey = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\NET Framework Setup\NDP\"))
            //{
            //    if (ndpKey != null)
            //    {
            //        string[] subKeyNames = ndpKey.GetSubKeyNames();
            //        foreach (string subKeyName in subKeyNames)
            //        {
            //            if (subKeyName.StartsWith("v"))
            //            {
            //                Console.WriteLine(".NET Version: " + subKeyName);
            //            }
            //        }
            //    }
            //}
        //            string versionToCheck = "v4.0"; // 要检查的.NET Framework版本

        //using (RegistryKey ndpKey = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\NET Framework Setup\NDP\"))
        //{
        //    if (ndpKey != null && ndpKey.OpenSubKey(versionToCheck) != null)
        //    {
        //        Console.WriteLine($".NET Framework {versionToCheck} 已安装。");
        //    }
        //    else
        //    {
        //        Console.WriteLine($".NET Framework {versionToCheck} 未安装。");
        //    }
        //}
            string assemblyPath = @"C:\erptestdll\MesBomUtility.dll /tlb:C:\erptestdll\MesBomUtility.tlb";

            // 构建注册COM组件的命令
            string regasmPath = Environment.ExpandEnvironmentVariables(@"%windir%\Microsoft.NET\Framework\v4.0.30319\regasm.exe");
            string arguments = String.Format("/codebase  {0}", assemblyPath);

            // 启动进程注册COM组件
            Process process = new Process();
            process.StartInfo.FileName = regasmPath;
            process.StartInfo.Arguments = arguments;
            process.StartInfo.Verb = "runas"; // 以管理员权限运行
            process.Start();
            process.WaitForExit();

            Console.WriteLine("COM组件注册完成。");

        }
        private void runbat()
        {
            Process process = new Process();
            process.StartInfo.FileName = @"c:\erptestdll\register.bat";
            process.StartInfo.UseShellExecute = false;
            process.StartInfo.RedirectStandardOutput = true;
            process.Start();

            // 可选：读取批处理文件的输出
            string output = process.StandardOutput.ReadToEnd();
            process.WaitForExit();

            // 输出批处理执行的结果
            System.Console.WriteLine(output);
        }
        private void installNetFramwork()
        {
            string dotNetInstallerPath = @"C:\Path\To\Your\DotNetFx40_Full_x86_x64.exe";

            // 启动进程安装.NET Framework 4.0
            Process process = new Process();
            process.StartInfo.FileName = dotNetInstallerPath;
            process.StartInfo.Arguments = "/q"; // 静默安装
            process.StartInfo.Verb = "runas"; // 以管理员权限运行
            process.Start();
            process.WaitForExit();

            Console.WriteLine(".NET Framework 4.0安装完成。");
        }

        private void textBox3_TextChanged(object sender, EventArgs e)
        {

        }
    }
}
